define(function (require, exports, module) {
  'use strict';
  var $ = require('jquery'),
      _ = require('underscore'),
      Backbone = require('backbone'),
      notify = require('js/utils/notify'),
      models = require('js/apps/op/models'),
      user_models = require('js/apps/console/models'),
      utils = require('js/utils/tools'),
      tz = require('js/utils/tz'),
      cookie = require('js/utils/cookie'),
      app = Backbone.history;
  require('datetimepicker');
  require('multiselect');
  require('magnific');
  require('jquery-ui');
  
  var MetisDailyBetView = Backbone.View.extend({
      events: {
          'click #search': 'doSearch',
          'click #closeBtn': 'closeUpdateLimitDialog',
          'click #saveBtn': 'setCurrentLimitCash'
      },
      
      initialize: function (options) {
          this.options = options;
          this.collection = new models.MetisDailyBetCollection();
          this.user_collection = new user_models.UserCollection();
          this.checkedIds = [];
          this.collection.bind('change remove reset', this.renderWithData, this);
      },
      render: function () {
          this.$el.html("");
          $(window).scrollTop(0);
          this.updateLimit();
          return this;
      },
      updateLimit: function () {
          var that = this;
          this.getCurrentLimitCash();
      },
      closeUpdateLimitDialog: function () {
          $("#newLimitCash").val('');
      },
      getCurrentLimitCash: function () {
          var that = this;
          $.ajax({
              url: '/admin/metis_daily_bet/',
              type: 'GET',
              headers: {
                  'X-AUTH-USER': cookie.getCookie('bigbang_user_id'),
                  'X-AUTH-TOKEN': cookie.getCookie('bigbang_user_token'),
                  'content-type': 'application/json'
              }
          }).done(function (resp) {
              var amount = resp.data.amount;
              setTimeout(() => {
                $("#currentlimitCash").text(amount)  
              }, 500);;
              
          }).fail(function (resp) {
              console.log(JSON.stringify(resp));
          });
      },
      setCurrentLimitCash: function () {
          var that = this;
          var amount = $("#newLimitCash").val() * 1;         
          if(amount>99999999) {
            notify.error('错误', '请输入正确数字范围0~99999999');
            return 
          } 

          $.ajax({
              url: '/admin/metis_daily_bet/',
              type: 'POST',
              headers: {
                  'X-AUTH-USER': cookie.getCookie('bigbang_user_id'),
                  'X-AUTH-TOKEN': cookie.getCookie('bigbang_user_token')
              },
              data: JSON.stringify({
                  'amount': amount 
              })
          }).done(function (resp) {
              notify.success('提示', '更新成功！');
              console.log(JSON.stringify(resp));
              $("#currentlimitCash").text($("#newLimitCash").val()); 
              that.closeUpdateLimitDialog();

          }).fail(function (resp) {
              notify.error('错误', '更新失败！');
              console.log(JSON.stringify(resp));
              that.closeUpdateLimitDialog();
          });
          
      },
      renderWithData: function () {
          this.$el.html(this.template({
              items: this.collection.toJSON(),
              searched: utils.getSearched('op/metis_daily_bet')
          }));
          var user_dict = {};
          _.each(this.user_collection.toJSON(), function (k) {
              user_dict[k.id] = k.nickname;
          })
          var that = this;
          
          utils.renderTable('main-list', {
              $orderby: that.options.$orderby || 'id',
              sortCallback: function (field) {
                  that.options.$orderby = field;
                  that.options.$page = 1;
                  var newUrl = utils.composeQueryString('#op/metis_daily_bet/', that.options);
                  app.navigate(newUrl, {
                      trigger: true
                  });
              }
          });
          $('.datetime-input').datetimepicker({
              format: 'YYYY-MM-DD HH:mm:ss',
          });
          $('.date-input').datetimepicker({
              format: 'YYYY-MM-DD',
          });
          utils.getPaginator(that.options, this.collection.total, '#op/metis_daily_bet/');
          return this;
      },
      load: function () {
          var that = this;
          this.user_collection.fetch({
            reset: true,
            data: {'disable_paginate': 1},
            success: function () {
                that.collection.fetch({
                    reset: true,
                    data: that.options,
                    error: function (c, r, o) {
                        notify.notifyResp(r);
                        $('#content').append('<h4 class="text-muted">无数据</h4>');
                    },
                });
            },
            error: function (c, r, o) {
                notify.notifyResp(r);
            }
          });
      }
  });
  exports.List = Backbone.View.extend({
      el: "#content_wrapper",
      template: "op/MetisDailyBet.html",
      initialize: function (options) {
          this.options = options;
      },
      render: function () {
          this.options.$size = 300;
          var view = new MetisDailyBetView(this.options);
          view.template = this.template;
          this.$el.empty();
          this.$el.append(view.render().el);
          view.load();
          
          return this;
      }
  });
  
});
